<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="660px" 
     @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :inline="true" :model="routerModel" ref="routerForm" 
      label-width="80px" label-position="right" size="small">
      <el-row>
         <el-col :span="8">
          <el-form-item prop="patient" label="被干预人">
            <el-input :maxlength="50" v-model="routerModel.patient"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="filenumber" label="档案号">
            <el-input  @blur="findbyMachs"  :maxlength="50" v-model="routerModel.filenumber" autofocus></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="survey" label="回访日期">
            <el-input :maxlength="50" v-model="routerModel.survey"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="way" label="方式">
            <el-input :maxlength="50" v-model="routerModel.way"></el-input>
          </el-form-item>          
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item prop="content" label="回访内容">
              <el-input :maxlength="50" v-model="routerModel.content"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="feedback" label="反馈信息">
             <el-input :maxlength="50" v-model="routerModel.feedback"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="result" label="处理结果">
             <el-input :maxlength="50" v-model="routerModel.result"></el-input>
          </el-form-item>          
        </el-col>
      
      </el-row>
      <el-row>
        <el-col>
          <el-form-item prop="staff" label="健康师">
             <el-input :maxlength="50" v-model="routerModel.staff"></el-input>
          </el-form-item>      
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('routerForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { savefollow,updatefollow,findBynumber } from '@/api/follow'

export default {
  patient: 'follow-edit',
  // props: ['parentRouterList'],
  data() {
    return {
      loading: false,
      routerModel: {
        id: 0,
        staff:"",
        filenumber: '',
        patient:"",
        way: '',
        content:'',
        feedback:'',
        result:'',
        survey:''
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
  
      // parentRouters: [
      //   {
      //     value: 0,
      //     label: '请选择'
      //   }        
      // ]   
    }
  },
  methods: {
    findbyMachs(){
      findBynumber(this.routerModel.filenumber)
      .then(response => {
        this.routerModel.patient=response.data.hName
      })
      .catch(error => {
        this.outputError(error)      
      })  
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['routerForm'].clearValidate()
        // this.$refs['routerCode'].focus()
      })
    },
    submitForm(formpatient) {
      this.$refs[formpatient].validate(valid => {
        if (valid) {          
          if(this.dialogProps.action === 'add') {
            this.doSaveRouter()
          } else {
            this.doUpdateRouter()
          }
        } else {
          return false;
        }
      });
    },    
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    doUpdateRouter() {
      this.loading = true
      updatefollow(this.routerModel.id, this.routerModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)      
      })      
    },
    doSaveRouter() {
      this.loading = true
      savefollow(this.routerModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)       
      })      
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })      
    }    
  },
  mounted: function() {
    this.$nextTick(() => {  
      this.$on('openEditfollowDialog', function(router) {
        this.routerModel = router
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改随访日记'
        this.dialogProps.visible = true
      })
      this.$on('openAddfollowDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加随访日记'
        this.routerModel = {
          id: 0,
          filenumber: '',
          patient:"",
          way: '',
          staff:"",
          content:'',
          feedback:'',
          result:'',
          survey:''
        }
        this.dialogProps.visible = true
      })
    })
  }    
}
</script>

<style lang="scss" scoped>
  .parent-router-select {
    width: 188px;
  }
  .properties-textarea {
    width: 496px;
  }
</style>


